package com.yjy.shmily;

import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;


import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.BaseCallback;
import com.yjy.moudle_imchart.ChatFragment;
import com.yjy.moudle_me.MeFragment;
import com.yjy.moudle_pair.PairFragment;
import com.yjy.moudle_square.SquareFragment;
import com.yjy.mybase.activity.MVPBaseActivity;
import com.yjy.mybase.mvp.MVPBasePresenter;
import com.yjy.mybase.utils.ARouterUri;
import com.yjy.mybase.utils.LogUtils;
import com.yjy.shmily.account.AccountHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUri.ACTIVITY_MAIN)
public class MainActivity extends MVPBaseActivity {

    @BindView(R.id.fl_main)
    FrameLayout flMain;
    @BindView(R.id.main_layout)
    ConstraintLayout mainLayout;
    @BindView(R.id.tv_pair)
    TextView tvPair;
    @BindView(R.id.tv_square)
    TextView tvSquare;
    @BindView(R.id.tv_chat)
    TextView tvChat;
    @BindView(R.id.tv_me)
    TextView tvMe;


    private ConstraintLayout constraintLayout;
    private boolean isDefaultTheme = true;

    /**
     * 四个Fragment
     */
    private PairFragment pairFragment;
    private SquareFragment squareFragment;
    private ChatFragment chatFragment;
    private MeFragment meFragment;

    /**
     * fm管理所需
     */
    private FragmentManager fm;
    private FragmentTransaction transaction;
    private Fragment currentFragment;


    @Override
    public int layout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        constraintLayout = findViewById(R.id.main_layout);

        initFragment();


        tvMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RippleAnimation.create(view).setmOnAnimationEndListener(new RippleAnimation.OnAnimationEndListener() {
                    @Override
                    public void onAnimationEnd() {

                    }
                }).start();

                checkTheme();

            }
        });

    }

    @Override
    public void initData() {
        SuperHttp.post("auth/login")
                .addParam("captcha", "123")
                .addParam("username", "dahai")
                .addParam("password", "123123")
                .request(new BaseCallback<LoginBean>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(LoginBean loginBean) {
                        LogUtils.e("MainActivity--", loginBean.toString());


                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        LogUtils.e("MainActivity--", errMsg);

                    }

                    @Override
                    public void onCompleted() {

                    }
                });

/**
 * cmd=GetPayInfo
 * uin=13389809
 * from=pc
 * buyuin=15468824
 * g_tk=827777256
 * callback=checkNumStatus2
 * random=0.45937308208208383
 */

/**
 * :status	200
 * date	Tue, 25 Feb 2020 04:04:37 GMT
 * content-type	text/plain;
 * server	QZHTTP-2.38.36
 * content-encoding	gzip
 */
        String num = "15468824";
        Map<String, String> map = new HashMap<>();
        map.put("cmd", "GetPayInfo");
        map.put("uin", "13389809");
        map.put("from", "pc");
        map.put("buyuin", num);
        map.put("g_tk", "827777256");
        map.put("callback", "checkNumStatus2");
        map.put("random", "0.45937308208208383");

        SuperHttp
                .get("https://cgi.haoma.qq.com/cgi-bin/HaomaWrap.fcgi?")
                .params(map)
                .addHeader("data", "Tue, 25 Feb 2020 04:04:37 GMT")
                .addHeader("content-type", "text/plain;")
                .addHeader("server", "QZHTTP-2.38.36")
                .addHeader("content-encoding", "gzip")
                .request(new BaseCallback<String>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(String data) {
                        LogUtils.e("main_mun_data--->" + data);

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        LogUtils.e("main_mun_err--->" + errMsg);
                    }

                    @Override
                    public void onCompleted() {

                    }
                });

    }

    private void checkTheme() {
        if (isDefaultTheme) {
            isDefaultTheme = false;
            constraintLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            tvMe.setTextColor(getResources().getColor(R.color.colorWhite));
        } else {
            isDefaultTheme = true;
            constraintLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            tvMe.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
        }


    }

    @Override
    public MVPBasePresenter createPresenter() {
        return null;
    }

    private void initFragment() {
        fm = getSupportFragmentManager();
        pairFragment = new PairFragment();
        squareFragment = new SquareFragment();
        chatFragment = new ChatFragment();
        meFragment = new MeFragment();


        seleFragment(0);

    }


    private void seleFragment(int i) {
        if (i >= 2 && !AccountHelper.isLogin()) {
            startActivity(new Intent(this, LoginActivity.class));
            return;
        }
        switch (i) {
            case 0:
                showFragment(pairFragment);
                break;
            case 1:
                showFragment(squareFragment);
                break;
            case 2:
                showFragment(chatFragment);
                break;
            case 3:
                showFragment(meFragment);
                break;


        }

//        switchTab(i)

    }


    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    private void showFragment(Fragment fg) {

        transaction = fm.beginTransaction();

        //如果之前没有添加过
        if (!fg.isAdded()) {
            if (null != currentFragment) {
                transaction
                        .hide(currentFragment)
                        .add(R.id.fl_main, fg);
            } else {
                transaction
                        .add(R.id.fl_main, fg);
            }

        } else {
            if (null != currentFragment) {
                transaction.hide(currentFragment);
                transaction.show(fg);
            } else {
                transaction.show(fg);

            }
        }

        //全局变量，记录当前显示的fragment
        currentFragment = fg;

        transaction.commit();
    }


    @OnClick({R.id.tv_pair, R.id.tv_square, R.id.tv_chat, R.id.tv_me})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_pair:
                seleFragment(0);
                break;
            case R.id.tv_square:
                seleFragment(1);
                break;
            case R.id.tv_chat:
                seleFragment(2);
                break;
            case R.id.tv_me:
                seleFragment(3);
                break;
        }
    }
}
